package top.hidoctor.auscultation.feature.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.BiaozhuListAdapter;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.AuditListBean;
import top.hidoctor.auscultation.bean.LabelListBean;
import top.hidoctor.auscultation.event.WorkYbEvent;
import top.hidoctor.auscultation.net.c.AuditListContract;
import top.hidoctor.auscultation.net.c.LabelListContract;
import top.hidoctor.auscultation.net.p.LabelListPresenter;
import top.hidoctor.auscultation.net.req.LabelReq;

public class GongzuotaiBzActivity extends BaseActivity implements LabelListContract.View {
    private RecyclerView recycler;
    private BiaozhuListAdapter adapter;
    private TextView tvTitle;
    private LabelListPresenter presenter;
    private LabelReq req;
    private EditText editSearch;
    private SwipeRefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_gongzuotai_list);
        EventBus.getDefault().register(this);
        presenter = new LabelListPresenter(this, this);
        req = new LabelReq();
        initView();

    }


    private void initView() {

        final Toolbar toolbar = findViewById(R.id.toolbar);
        tvTitle = findViewById(R.id.tv_title);
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        tvTitle.setText("标注工作台");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSoft(toolbar);
                onBackPressed();
            }
        });
        recycler = findViewById(R.id.recycler);
        editSearch = findViewById(R.id.edit_search);
        refreshLayout = findViewById(R.id.refreshLayout);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter = new BiaozhuListAdapter(R.layout.item_biaozhu_gongzuotai, null));
        recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (adapter.getData().get(position).getStatus() == 30) {
                    showToast("标注已超时");
                    return;
                }
                Intent intent = new Intent(GongzuotaiBzActivity.this, WorkCenterBzActivity.class);
                intent.putExtra("id", adapter.getData().get(position).getId());
                intent.putExtra("name", adapter.getData().get(position).getName());
                intent.putExtra("status", adapter.getData().get(position).getStatus() + "");
                startActivity(intent);
            }
        });

        View view = LayoutInflater.from(this).inflate(R.layout.layout_empty_list, (ViewGroup) recycler.getParent(), false);
        adapter.setEmptyView(view);
        adapter.isFirstOnly(false);
        adapter.openLoadAnimation();
        adapter.openLoadMore(10, true);
        recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));


        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        });
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                hideSoft(recycler);
                refresh();
            }
        });

        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                //以下方法防止两次发送请求
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    switch (event.getAction()) {
                        case KeyEvent.ACTION_UP:
                            //发送请求
                            req.setKeyWord(v.getText().toString().trim());
                            refresh();
                            return true;
                        default:
                            return true;
                    }
                }
                return false;
            }
        });
        refresh();
    }

    /**
     * 上啦加载
     */
    private void loadMore() {
        req.setPageNo(1 + req.getPageNo());
        presenter.getList(req, refreshLayout);
    }

    /**
     * 下啦刷新
     */
    private void refresh() {
        req.setPageNo(1);
        presenter.getList(req, refreshLayout);
    }

    /**
     * 列表数据获取回调
     * @param bean
     */
    @Override
    public void getListSuc(LabelListBean bean) {
        if (req.getPageNo() == 1) {
            refreshLayout.setRefreshing(false);
            adapter.setNewData(bean.getResult());
        } else {
            adapter.addData(bean.getResult());
        }
        adapter.openLoadMore(bean.getTotalItems() > adapter.getItemCount());
    }


    /**
     * 通知刷新
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(WorkYbEvent event) {
        refresh();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onError(String code, String msg) {
        super.onError(code, msg);
        showToast(msg);
        refreshLayout.setRefreshing(false);
    }
}
